package com.example.communist_app_api.controller.web;

import com.example.communist_app_api.config.mq.NoticeTask;
<<<<<<< HEAD
import com.example.communist_app_api.db.pojo.User;
<<<<<<< HEAD
import com.example.communist_app_api.enums.SenderNameEnum;
=======
import com.example.communist_app_api.db.pojoDto.User;
>>>>>>> 党建uniapp/dev
=======
>>>>>>> parent of aec1d46 (消息模块的搭建和配合其他模块消息的发送和收取)
import com.example.communist_app_api.enums.StatusEnum;
import com.example.communist_app_api.exception.GlobalException;
import com.example.communist_app_api.service.RoleService;
import com.example.communist_app_api.service.UserOrganizationService;
import com.example.communist_app_api.service.UserRoleService;
import com.example.communist_app_api.service.UserService;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.web.req.AuditPersonalMessageAuditReq;
import com.example.communist_app_api.vo.web.req.AuditPersonalMessageShowReq;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/2/20 17:31
 */
@RestController
@RequestMapping("/audit")
public class AuditController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserOrganizationService userOrganizationService;
    @Autowired
    private NoticeTask noticeTask;

    public static final String roleName = "党员";

    //显示审批游客转普通党员
    @ApiOperation("分页查询审核申请")
    @PostMapping("/AuditPersonalMessage/show")
    public R AuditPersonalMessageShow(@RequestBody AuditPersonalMessageShowReq req ){
        PageInfo<User> pageInfo = userService.findPageByAudit(req);
        return R.ok().put("pageInfo",pageInfo);
    }

    //审批
    @ApiOperation("审批成功")
    @PostMapping("/AuditPersonalMessage/auditSuccess")
    public R AuditPersonalMessageAuditSuccess(@RequestBody AuditPersonalMessageAuditReq req){
        Integer userId = req.getUserId();
        User user = new User();
        user.setId(userId);
        user.setStatus(StatusEnum.NORMAL.getCode());
        userService.editPersonMessage(user);
        //查询角色党员的id
        Integer roleId = roleService.selectRoleIdByRoleName(roleName);
        if (roleId == 0){
            throw new GlobalException("查询角色Id信息失败，请确定角色名称是否正确");
        }
        //审核通过变成正常状态，角色role由游客变成党员
        Integer updateNum = userRoleService.updateUserRole(userId,roleId);
        if (updateNum == 0) {
            throw new GlobalException("更新角色信息失败");
        }
        String organizationName = userOrganizationService.selectOrganizationNameByUserId(userId);
        String message = "欢迎你加入"+organizationName+"!";
        noticeTask.sendSystemNotice(String.valueOf(req.getUserId()),message);
        return R.ok(message);
    }

    @ApiOperation("审批不通过")
    @PostMapping("/AuditPersonalMessage/auditFail")
    public R AuditPersonalMessageAuditFail(@RequestBody AuditPersonalMessageAuditReq req){
        String message = "";
        if (req.getMessage() == null) {
             message = "加入组织审批不通过,请重新整理资料，无误后提交申请";
        }else {
             message = req.getMessage();
        }
        User user = new User();
        user.setId(req.getUserId());
        //审核不通过重新打回游客状态
        user.setStatus(StatusEnum.VISITOR.getCode());
        userService.editPersonMessage(user);
        noticeTask.sendSystemNotice(String.valueOf(req.getUserId()),message);
        return R.ok(message);
    }
}
